/**
 * 字典数据库操作文件
 */

const { query, queryFirst } = require('./common');
const { DICTIONARY } = require('../utils/CONST');

/**
 * 根据类型获取数据列表
 * @returns 
 */
const getListByType = type => query('SELECT * FROM dictionary WHERE ? = 0 OR type = ? ORDER BY type,id', [type, type]);

/**
 * 添加字典
 * @param {*} dictionary 要添加的字典数据
 * @returns 
 */
const addDictionary = dictionary => query('INSERT INTO dictionary SET ?', dictionary);

/**
 * 修改字典
 * @param {*} dictionary 要修改的字典数据
 * @returns 
 */
const updateDictionary = dictionary => {
    const { id } = dictionary;
    delete dictionary.id;
    return query('UPDATE dictionary SET ? WHERE id = ?', [dictionary, id]);
}

module.exports = {
    getListByType,
    addDictionary,
    updateDictionary
}